﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;


namespace Giaiphongmatbang.BLL
{
    public class GravesRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<Grave> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Graves
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.GraveName).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public Grave GetByID(int ID)
        {
            var result = from p in data.Graves
                         where p.GraveID == ID  
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<Grave> GetByParentID(int ID)
        {
            return data.Graves.Where(p => p.ParentID == ID);
        }
        public IEnumerable<Grave> GetByCMND(string cmnd)
        {
            return data.Graves.Where(p => p.InventoryGraves.Any(q => q.CMND == cmnd));
        }

        public IEnumerable<Grave> GetByProjectID(int ID)
        {
            return data.Graves.Where(p => p.InventoryGraves.Any(q => q.ProjectID == ID));
        }

        public bool HasChild(int ID)
        {
            return data.Graves.Any(p => p.ParentID == ID);
        }
        public void Insert(Grave g)
        {
            data.AddToGraves(g);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }

    }
}